package com.zyd.dao;

import com.zyd.service.power.impl.PowerServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 车辆与围栏中间表
 *
 * @Author: caicaicainiaoa
 * @Date: 2019/4/3  15:56
 */
@Repository
public class EnclosureVehicleDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(EnclosureVehicleDao.class);


    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询车辆的当前车辆所在的围栏
     * @param tagCode
     * @return 如果是null 那是为查询到数据 需要插入,如果是0 那是车辆未受围栏管理
     */
    public Integer findEnclosureIdByTagCode(String tagCode) {
        Integer enclosureId =null;
        try {
            String sql ="SELECT enclosure_id FROM zydzn_enclosure_vehicle WHERE tag_code = ? ";
            enclosureId= jdbcTemplate.queryForObject(sql,Integer.class,tagCode);
        }catch (Exception e){
            LOGGER.info("根据tag_code:"+tagCode+",未查询到对应的所属围栏!");
        }
        return enclosureId;
    }

    /**
     * 插入一条数据
     * @param enclosureId
     * @param tagCode
     */
    public void insert(Integer enclosureId, String tagCode) {
        String sql = " INSERT INTO zydzn_enclosure_vehicle (enclosure_id,tag_code) VALUES (? ,? ) ";
        jdbcTemplate.update(sql,enclosureId,tagCode );
    }

    /**
     * 根据 tagCode 修改 enclosureId
     * @param enclosureId
     * @param tagCode
     */
    public void updateEnclosureIdByTagCode(Integer enclosureId, String tagCode) {
        String sql="UPDATE zydzn_enclosure_vehicle SET enclosure_id = ? WHERE tag_code = ? ";
        jdbcTemplate.update(sql,enclosureId,tagCode );
    }
}
